package com.kintreda.ieco_server.module.file.web;

import com.kintreda.ieco_server.bean.document.Attachment;
import com.kintreda.ieco_server.bean.document.Document;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.file.service.IAttachmentService;
import com.kintreda.ieco_server.module.file.service.IDocumentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * material 素材管理
 *
 * @Project ieco_server
 * @Package com.kintreda.ieco_server.module.file.web
 * @Authro HuangZhongYao
 * @PC-User newUser
 * @DATE 2019年06月09号
 * @Date-ZH 六月 星期日
 */
@Controller
@RequestMapping("/material")
public class MaterialController {
    @Value("#{properties['server.imgurl']}")
    String imgUrl;

    @Autowired
    IDocumentService documentService;
    @Autowired
    IAttachmentService attachmentService;
    @Autowired
    IBaseRepository<Document,String> documentStringIBaseRepository;

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("material/material-list").addObject("imgurl",imgUrl);
    }

    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public Object query(String name){

        StringBuffer hql = new StringBuffer("select t1.*,t2.name as nameA from document t1, attachment t2 where  t1.attachId = t2.id and  t1.status =  ? ");
        List<Object>  params = new ArrayList<>();
        params.add("0");
        if (StringUtils.isNotBlank(name)){
            hql.append(" and t1.name like ?");
            params.add("%"+name+"%");
        }
        hql.append(" order by t1.createDate desc ");
        return documentStringIBaseRepository.findListBySql(hql.toString(),params.toArray());
    }


    @RequestMapping("/add")
    public ModelAndView add(){
         return new ModelAndView("material/material-add");
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(Document document){
        Document documentDB = documentService.get(document.getId());
        documentDB.setName(document.getName());
        documentDB.setRemark(document.getRemark());
        documentDB.setStatus("0");
        documentService.update(documentDB);
        return R.ok("添加成功!");
    }


    @RequestMapping("/edit")
    public ModelAndView edit(String id){
        Document document = documentService.get(id);
        Attachment attachment = attachmentService.get(document.getAttachId());
        ModelAndView modelAndView = new ModelAndView("material/material-edit");
        modelAndView.addObject("document",document);
        modelAndView.addObject("attachment",attachment);
        return modelAndView;
    }

    @RequestMapping("/det")
    public ModelAndView det(String id){
        Document document = documentService.get(id);
        Attachment attachment = attachmentService.get(document.getAttachId());
        ModelAndView modelAndView = new ModelAndView("material/material-det");
        modelAndView.addObject("document",document);
        modelAndView.addObject("attachment",attachment);
        return modelAndView;
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(Document document){
        Document documentDB = documentService.get(document.getId());
        if (documentDB != null) {
            documentDB.setRemark(document.getRemark());
            documentDB.setName(document.getName());
            documentService.update(documentDB);
            return R.ok("修改成功");
        }
        return R.error("修改数据不存在!");
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(String id){
        Document document = documentService.get(id);
        document.setStatus("1");
        documentService.update(document);
        return R.ok("删除成功");

    }


}
